package org.hxzon.blogbackup.baidu;

import java.io.File;
import java.io.IOException;
import java.util.List;

import org.apache.commons.io.FileUtils;
import org.hxzon.util.DebugUtil;

public class BlogWriter {

    public static String encoding = "UTF-8";
    public static String newLine = "\n";
    public static String ext = ".html";
    //directory
    public static String pageInfoDirectory = "pageInfo/";
    public static String itemDirectory = "item/";
    public static String imageInfoDirectory = "imageInfo/";
    //url
    public static String baseUrl = "http://hi.baidu.com";
    public static String hxzonUrl = baseUrl + "/hxzon/blog";
    public static String indexBaseUrl = hxzonUrl + "/index/";

    public static String readPageInfo(int index) {
        String fileName = pageInfoDirectory + index + ext;
        String origContent;
        try {
            origContent = FileUtils.readFileToString(new File(fileName), encoding);
            return origContent;
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    public static String readImageInfo(int index) {
        String fileName = imageInfoDirectory + index + ext;
        String origContent;
        try {
            origContent = FileUtils.readFileToString(new File(fileName), encoding);
            return origContent;
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    public static String readItem(BlogFile file) {
        String fileName = itemDirectory + file.getFullPath();
        String origContent;
        try {
            origContent = FileUtils.readFileToString(new File(fileName), encoding);
            return origContent;
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    public static void writePageInfo(BlogPage page) {
        int index = page.getIndex();
        String fileName = pageInfoDirectory + index + ext;
        StringBuilder sb = new StringBuilder();
        sb.append("<html>");
        sb.append("<meta http-equiv='content-type' content='text/html; charset=UTF-8'>");
        sb.append("<head><title>hxzon-");
        sb.append(index);
        sb.append("</title></head><body>");
        sb.append("<a href='http://hi.baidu.com/hxzon/blog/index/").append(index).append("'>").append("page:").append(index).append("</a>");
        sb.append("<br />");
        sb.append("<a href='").append(index - 1).append(ext).append("'>").append(index - 1).append("</a>");
        sb.append("-----");
        sb.append("<a href='").append(index + 1).append(ext).append("'>").append(index + 1).append("</a>");
        for (BlogFile file : page.getBlogFiles()) {
            sb.append("<div class='item'>");
            sb.append("<p class='tit'>").append(file.getTitle().html()).append("</p>");
            sb.append("<p class='url'>").append(file.getOrigUrl()).append("</p>");
            sb.append("<p class='cate'>").append(file.getCategoryHtml()).append("</p>");
            sb.append("<p class='date'>").append(file.getDate().html()).append("</p>");
            sb.append("</div>");
        }
        sb.append("</body></html>");
        try {
            FileUtils.writeStringToFile(new File(fileName), sb.toString(), encoding);
        } catch (IOException e) {
            DebugUtil.error("write pageinfo err," + index + "," + e.getMessage(), e);
        }
    }

    public static void writeImageInfo(List<BlogImage> images, int index) {
        StringBuilder sb = new StringBuilder();
        sb.append("<html>");
        sb.append("<meta http-equiv='content-type' content='text/html; charset=UTF-8'>");
        sb.append("<head><title>");
        sb.append("hxzon-image-");
        sb.append(index);
        sb.append("</title></head><body>");
        sb.append("page:").append(index);
        sb.append("<br />");
        sb.append("<a href='").append(index - 1).append(ext).append("'>").append(index - 1).append("</a>");
        sb.append("-----");
        sb.append("<a href='").append(index + 1).append(ext).append("'>").append(index + 1).append("</a>");
        for (BlogImage image : images) {
            sb.append("<div class='item'>");
            //get from orig,write to category+"/"+filePath
            sb.append("<p class='category'>").append(image.getFile().getCategoryHtml()).append("</p>");
            sb.append("<p class='file'>").append(image.getFilePath()).append("</p>");
            sb.append("<p class='orig'>").append(image.getSrc()).append("</p>");
            sb.append("</div>");
        }
        sb.append("</body></html>");
        String fileName = imageInfoDirectory + index + ext;
        try {
            FileUtils.writeStringToFile(new File(fileName), sb.toString(), encoding);
        } catch (Exception e) {
            DebugUtil.error("write imageinfo err," + fileName + ":" + e.getMessage(), e);
        }
    }

    public static void writeItem(BlogFile item) {
        try {
            FileUtils.writeStringToFile(new File(itemDirectory + item.getFullPath()), item.getTotalContent(), encoding);
        } catch (Exception e) {
            DebugUtil.error("write file err," + item.getTitle().text() + ":" + e.getMessage(), e);
        }
    }

    public static boolean isItemExist(BlogFile item) {
        return new File(itemDirectory + item.getFullPath()).exists();
    }
}
